import React, { Component, Fragment } from 'react';
import { connect } from 'dva';
import { AddressSelector } from 'antd-pro-biz';
import { Icon } from 'antd';
import api from '@/api';
import { ADDRESS_TYPE, TRANSPORT_TYPE } from '@/Constants';
import styles from './index.less';
import _ from 'lodash';

@connect(({ }) => ({
}))
class AddressNew extends Component {
  constructor(props) {
    super(props);
    this.state = {
      groups: [],
      selectedItems: [],
    };
  }

  componentDidMount() {
    this.initGroups();
  }

  handleSearchGroup = params => {
    const { type, freightChargeData } = this.props;
    const { addressType, isProduct, nodeId, productId, idType } = params;
    if (!isProduct) {
        return api.address.addressGroup();
    } else {
      if (type === ADDRESS_TYPE.PICK_UP && freightChargeData && freightChargeData.shipper) {
        let shipper = freightChargeData.shipper
        let _shipper = shipper && shipper[shipper.length - 1];
        params.code = _shipper.code;
        params.group = _shipper.groupCode;
        params.areaType = _shipper.addressType;
      } else if (type === ADDRESS_TYPE.DELICIOUS && freightChargeData && freightChargeData.consignee) {
        let consignee = freightChargeData.consignee
        let _consignee = Array.isArray(freightChargeData.consignee) ? consignee[consignee.length - 1] : consignee;
        params.code = _consignee.code || consignee.geoRange1;
        params.group = _consignee.groupCode || 0;
        params.areaType = _consignee.addressType;
      }
      if (idType == 2) {
        params.serviceId = nodeId ? nodeId : productId;
        return api.address.serviceAddressGroup({ params });
      } else {
        return api.address.orderAddressGroup({ params });
      }
    }
  };

  handleSearch = params => {
    console.log('handleSearch', params)
    const { addressType, isProduct, productId, resultType, nodeId, type, freightChargeData } = this.props;
    params.addressType = addressType;
    params.pageSize = 10;
    params.type = type;
    if (!isProduct) {
      return api.address.addressSearch({ params });
    // } else {
    //   if (type === ADDRESS_TYPE.PICK_UP && freightChargeData && freightChargeData.shipper) {
    //     let shipper = freightChargeData.shipper
    //     let _shipper = shipper && shipper[shipper.length - 1];
    //     params.code = _shipper.code;
    //     params.group = _shipper.groupCode;
    //     params.areaType = _shipper.addressType;
    //   } else if (type === ADDRESS_TYPE.DELICIOUS && freightChargeData && freightChargeData.consignee) {
    //     let consignee = freightChargeData.consignee
    //     let _consignee = Array.isArray(freightChargeData.consignee) ? consignee[consignee.length - 1] : consignee;
    //     params.code = _consignee.code || consignee.geoRange1;
    //     params.group = _consignee.groupCode || 0;
    //     params.areaType = _consignee.addressType;
    //   }

      // if (resultType == 2) {
      //   params.serviceId = nodeId ? nodeId : productId;
      //   return api.address.serviceAddressSearch({ params });
      // } else {
      //   params.productId = productId;
      //   params.nodeId = nodeId;
      //   return api.address.orderAddressSearchNew({ params });
      // }
    }
  };

  async initGroups() {
    let { groups } = this.state;
    const { type, addressType, isProduct, resultType, nodeId, productId, maxLevel } = this.props;
    // 因为暂时不要国际，所以先注释“国际”，固定为中国
    // let resGroup = await this.handleSearchGroup({
    //   type,
    //   addressType,
    //   isProduct,
    //   idType: resultType,
    //   nodeId,
    //   productId,
    // });
    // if (resGroup.errorCode === 0) {
    //   groups = resGroup.data.map(g => {
    //     if (g.code == '0') {
    //       maxLevel ? g.maxLevel = maxLevel :  addressType == TRANSPORT_TYPE.AIR ? g.maxLevel = 3 : g.maxLevel = 4;
    //     } else if (g.code == '1') {
    //       g.maxLevel = 3
    //     } else {
    //       g.maxLevel = 2;
    //     }
    //     return g;
    //   });
    // }

    //  XIAN_GROUP code=0是中国 自己接口code=1是中国
    groups = [{code: "0", name: "中国", maxLevel: 4}].map(g => {
      g.maxLevel = maxLevel
      return g;
    });
    this.setState({ groups });
  }

  handleChange = selectedItems => {
    const { onChange, clearProducts } = this.props;
    if (onChange) {
      clearProducts && clearProducts()
      onChange(selectedItems);
    }
    // this.setState({selectedItems})
  };
  renderInputValue = (items) => {
    const { type, addressType, isProduct } = this.props;
    if (_.isEmpty(items)) return '';
    let lastItem = items[items.length - 1];
    let itemsNames = items.map(n => n.name).join('-');
    let lastItemNames = lastItem.name;
    if (lastItem.groupCode == 0) {
      if (addressType == TRANSPORT_TYPE.AIR && lastItem.level == 3) {
        return lastItemNames;
      } else {
        return itemsNames;
      }
    } else {
      if (addressType == TRANSPORT_TYPE.AIR && lastItem.level == 2) {
        return lastItemNames;
      } else {
        return itemsNames;
      }
    }


    // if (lastItem.level == 4) {
    //   return lastItem.name;
    // } else {
    //   return '';
    // }
  };

  render() {
    const { groups } = this.state;
    const { type, value, placeholder, addonAfterHide, addonAfter, selectorStyle, contentStyle, disabled } = this.props;
    return (
      <AddressSelector
        type={type}
        value={value}
        topTabData={groups}
        tabData
        onSearch={this.handleSearch}
        colSpan={8}
        inputProps={{
          disabled: disabled,
          // placeholder: placeholder || '请选择地址',
          style: { overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', background: 'transparent' },
          addonAfter: !addonAfterHide && (addonAfter || <Icon type="ellipsis"/>),
          renderValue: this.renderInputValue,
        }}
        onChange={this.handleChange}
        contentStyle={contentStyle}
        style={{ width: '100%', ...selectorStyle }}
        hint="温馨提示：支持中文、拼音或首字母，如：西安 或 XA"
      />
    );
  }
}

export default AddressNew;
